package com.sander.guardmanagement.ui.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.ui.model.CommuityDetailGroupModel;
import com.sander.guardmanagement.ui.model.DetailUnitModel;
import com.sander.guardmanagement.ui.model.DistrictItem;
import com.sander.guardmanagement.ui.view.DistrictGroupItem;
import com.sander.guardmanagement.widgets.AnimatedExpandableListView;

import java.util.List;

/**
 * Created by sander on 2017/4/13.
 */

public class DistrictExpandAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {

    public List<CommuityDetailGroupModel> groupList;

    private Context context;

    private LayoutInflater inflater;


    public DistrictExpandAdapter(Context context, List<CommuityDetailGroupModel> list)
    {
        this.context = context;
        this.groupList = list;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        DistrictItem item = null;
        if (convertView == null){
            convertView = this.inflater.inflate(R.layout.districtunititem,parent,false);
            TextView textView = (TextView) convertView.findViewById(R.id.unitTextView);
            item = new DistrictItem();
            item.setTextView(textView);
            convertView.setTag(item);
        }else {
            item = (DistrictItem) convertView.getTag();
        }
        DetailUnitModel model = this.groupList.get(groupPosition).unitList.get(childPosition);
        item.getTextView().setText(model.unitTitleString);
        return convertView;
    }

    @Override
    public int getRealChildrenCount(int groupPosition) {
        return this.groupList.get(groupPosition).unitList.size();
    }

    @Override
    public int getGroupCount() {
        return this.groupList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {

        return this.groupList.size();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.groupList.get(groupPosition).unitList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        DistrictGroupItem item = null;
        if (convertView == null){
            convertView = this.inflater.inflate(R.layout.districtgroupitem,parent,false);
            item = new DistrictGroupItem();
            TextView textView = (TextView) convertView.findViewById(R.id.districtTextView);
            item.setTitleView(textView);
            convertView.setTag(item);
        }else{
            item = (DistrictGroupItem) convertView.getTag();
        }

        CommuityDetailGroupModel model = this.groupList.get(groupPosition);
        item.getTitleView().setText(model.groupTitle);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
